package com.kede.app.service.impl;

import com.kede.app.domain.Supplies;
import com.kede.app.repository.SuppliesRepository;
import com.kede.app.service.StoreUserOrderService;
import com.kede.app.service.SuppliesService;
import com.kede.app.service.dto.SuppliesDto;
import com.kede.app.service.dto.SuppliesQueryCriteria;
import com.kede.app.service.mapstruct.SuppliesMapper;
import com.kede.utils.*;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author Pr.li
* @date 2023-06-06
**/
@Service
@RequiredArgsConstructor
public class SuppliesServiceImpl implements SuppliesService {

    private final SuppliesRepository suppliesRepository;
    private final SuppliesMapper suppliesMapper;
    private final StoreUserOrderService userOrderService;

    @Override
    public Map<String,Object> queryAll(SuppliesQueryCriteria criteria, Pageable pageable){
        Page<Supplies> page = suppliesRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        page.getContent().forEach(supplies -> {
            Integer count = userOrderService.getStoreUserOrderNum(supplies.getId());
            supplies.setExchangeNum(count);
        });
        return PageUtil.toPage(page.map(suppliesMapper::toDto));
    }


    @Override
    public List<SuppliesDto> queryAll(SuppliesQueryCriteria criteria){

        return suppliesMapper.toDto(suppliesRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public SuppliesDto findById(Integer id) {
        Supplies supplies = suppliesRepository.findById(id).orElseGet(Supplies::new);
        ValidationUtil.isNull(supplies.getId(),"Supplies","id",id);
        return suppliesMapper.toDto(supplies);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SuppliesDto create(Supplies resources) {
        String currentUsername = SecurityUtils.getCurrentUsername();
        resources.setCreateBy(currentUsername);
        resources.setUpdateBy(currentUsername);
        resources.setCurrNum(resources.getNum());
        return suppliesMapper.toDto(suppliesRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Supplies resources) {
        Supplies supplies = suppliesRepository.findById(resources.getId()).orElseGet(Supplies::new);
        ValidationUtil.isNull( supplies.getId(),"Supplies","id",resources.getId());
        supplies.copy(resources);
        suppliesRepository.save(supplies);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            suppliesRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<SuppliesDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SuppliesDto supplies : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("物资名称", supplies.getSupplieName());
            map.put("数量", supplies.getNum());
            map.put("交付图片信息", supplies.getDeliveryImg());
            map.put("渠道", supplies.getChannel());
            map.put("展示图片，给用户看的", supplies.getShowImg());
            map.put(" createBy",  supplies.getCreateBy());
            map.put(" updateBy",  supplies.getUpdateBy());
            map.put(" createTime",  supplies.getCreateTime());
            map.put(" updateTime",  supplies.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}